package com.yuncommunity.cloudlab.utils

import com.wf.captcha.SpecCaptcha
import com.wf.captcha.base.Captcha
import net.bytebuddy.utility.RandomString
import java.util.*
import java.util.concurrent.ThreadLocalRandom
import kotlin.collections.HashMap
import kotlin.concurrent.schedule

object MyCaptcha {
    var imageMap = HashMap<String, String>()
    var smsMap = HashMap<String, String>()

    fun imageCodeVerify(key: String, value: String): Boolean {
        if (imageMap.containsKey(key)) {
            var result = imageMap[key] == value
            imageMap.remove(key)
            return result
        }
        return false
    }

    fun newImageCaptcha(): Map<String, String> {
        var specCaptcha = SpecCaptcha(130, 36, 4)
        specCaptcha.charType = Captcha.TYPE_ONLY_NUMBER
        var result = HashMap<String, String>()
        var captchaId = RandomString.make(8)
        var value = specCaptcha.text()
        result.put("captchaId", captchaId)
        result.put("image", specCaptcha.toBase64())
        imageMap.put(captchaId, value)
        Timer().schedule(5 * 60 * 1000) {
            imageMap.remove(captchaId)
        }
        return result
    }

    fun smsCodeVerify(phone: String, vCode: String): Boolean {
        if (smsMap.containsKey(phone)) {
            var result = smsMap.get(phone) == vCode
            smsMap.remove(phone)
            return result
        }
        return false
    }

//    fun sendSmsCode(phone: String, mySettings: MySettings): String {
//        var randomCode = ThreadLocalRandom.current().nextInt(1000, 9999)
//        smsMap.put(phone, randomCode.toString())
//
//        Timer().schedule(5 * 60 * 1000) {
//            smsMap.remove(phone)
//        }
//        return MySms.sendSms(phone, randomCode.toString(), mySettings)
//    }
}